package com.sskj.mine;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.base.BaseActivity;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * @author Hey
 * Create at  2019/05/01
 */
public class BindMobileActivity extends BaseActivity<BindMobilePresenter> {


    @BindView(R2.id.mobile_edt)
    EditText mobileEdt;
    @BindView(R2.id.code_edt)
    EditText codeEdt;
    @BindView(R2.id.pwd_edt)
    EditText pwdEdt;
    @BindView(R2.id.get_code_tv)
    TextView getCodeTv;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    private Boolean isBindMobile;
    private DisposableSubscriber<Long> disposableSubscriber;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_bind_mobile;
    }

    @Override
    public BindMobilePresenter getPresenter() {
        return new BindMobilePresenter();
    }

    @Override
    public void initView() {
        isBindMobile = getIntent().getBooleanExtra("isBindMobile", true);
        setTitle(isBindMobile ? App.INSTANCE.getString(R.string.mine_bindMobileActivity1_j) : App.INSTANCE.getString(R.string.mine_bindMobileActivity2_j));
        mobileEdt.setHint(isBindMobile ? App.INSTANCE.getString(R.string.mine_bindMobileActivity3_j) : App.INSTANCE.getString(R.string.mine_bindMobileActivity4_j));
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initEvent() {

        ClickUtil.click(getCodeTv, () -> {
            if (isEmpty(mobileEdt)) {
                ToastUtil.showShort(isBindMobile ? App.INSTANCE.getString(R.string.mine_bindMobileActivity1_j) : App.INSTANCE.getString(R.string.mine_bindMobileActivity4_j));
                return;
            }
            if (isBindMobile) {  // 手机号
                if (!CheckUtil.isMobile(getText(mobileEdt))) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_bindMobileActivity5_j));
                    return;
                }
            } else {
                if (!CheckUtil.isEmail(getText(mobileEdt))) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_bindMobileActivity6_j));
                    return;
                }
            }

            if (getText(mobileEdt).contains("@")) {
                mPresenter.sendEmail(getText(mobileEdt));
                return;
            }
            mPresenter.sendCode(getText(mobileEdt));
        });

        ClickUtil.click(submitBtn, () -> {
            if (isEmpty(mobileEdt)) {
                ToastUtil.showShort(isBindMobile ? App.INSTANCE.getString(R.string.mine_bindMobileActivity3_j) : App.INSTANCE.getString(R.string.mine_bindMobileActivity4_j));
                return;
            }

            if (isEmpty(codeEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_settingPasswordActivity15_j));
                return;
            }
            if (isBindMobile) {
                if (!CheckUtil.isMobile(getText(mobileEdt))) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_bindMobileActivity5_j));
                    return;
                }
            } else {
                if (!CheckUtil.isEmail(getText(mobileEdt))) {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_bindMobileActivity6_j));
                    return;
                }
            }

            if (isEmpty(pwdEdt)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_transferActivity4_j));
                return;
            }
            if (!CheckUtil.isPayPwd(getText(pwdEdt))) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.mine_transferActivity4_j));
                return;
            }
            mPresenter.bindMobile(getText(mobileEdt), getText(codeEdt), getText(pwdEdt));
        });
    }

    public void sendCodeSuccess() {
        startTimeDown(getCodeTv);
    }


    public static void start(Context context, boolean isBindMobile) {
        Intent intent = new Intent(context, BindMobileActivity.class);
        intent.putExtra("isBindMobile", isBindMobile);
        context.startActivity(intent);
    }

    public void onAuthCodeSuccess() {
        startCount();
    }

    public void startCount() {
        getCodeTv.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                if (getCodeTv != null) {
                    getCodeTv.setText(time + App.INSTANCE.getString(R.string.mine_settingPasswordActivity16_j));
                }
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                if (getCodeTv != null) {
                    getCodeTv.setText(App.INSTANCE.getString(R.string.mine_withdrawActivity8_j));
                    getCodeTv.setEnabled(true);
                }
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }
}
